package elm.dao.impl;

import elm.dao.BusinessDao;
import elm.pojo.Business;
import util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;
    @Override
    public Business login(Integer businessId, String password) {
        Business business = null;
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("select * from business where businessId=? and password=?");
            pstmt.setInt(1,businessId);
            pstmt.setString(2,password);
            rs = pstmt.executeQuery();
            if (rs.next()){
                business = new Business(rs.getInt(1),rs.getString(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getDouble(6),rs.getDouble(7));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return business;
    }

    @Override
    public Business findBusinessByBusinessId(Integer businessId) {
        Business business = null;
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("select * from business where businessId=?");
            pstmt.setInt(1,businessId);
            rs = pstmt.executeQuery();
            if (rs.next()){
                business = new Business(rs.getInt(1),rs.getString(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getDouble(6),rs.getDouble(7));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return business;
    }

    @Override
    public List<Business> findAllBusiness() {
        List<Business> businesses = new ArrayList<>();
        Business business = null;
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("select * from business");
            rs = pstmt.executeQuery();
            while (rs.next()){
                business = new Business(rs.getInt(1),rs.getString(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getDouble(6),rs.getDouble(7));
                businesses.add(business);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return businesses;
    }

    @Override
    public List<Business> likeBusiness(String businessName, String businessAddress) {
        List<Business> businesses = new ArrayList<>();
        Business business = null;
        try {
            conn = DruidUtil.getConnection();
            StringBuffer sql = new StringBuffer("select * from business ");
            if(businessName!=null && !businessName.equals("")){
                sql.append("where businessName like '%"+businessName+"%' ");
            }
            if(businessAddress!=null && !businessAddress.equals("")){
                sql.append("where businessAddress like '%"+businessAddress+"%' ");
            }
            pstmt = conn.prepareStatement(sql.toString());
            rs = pstmt.executeQuery();
            while (rs.next()){
                business = new Business(rs.getInt(1),rs.getString(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getDouble(6),rs.getDouble(7));
                businesses.add(business);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return businesses;
    }

    @Override
    public Integer addBusiness(String businessName) {
        Integer businessId = 0;

        try {
            conn = DruidUtil.getConnection();
            String sql = "insert into business(businessName,password) values(?,'123')";
            pstmt = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pstmt.setString(1,businessName);
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();
            if (rs.next()){
                businessId =rs.getInt(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return businessId;
    }

    @Override
    public void deleteBusiness(Integer businessId) {
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("delete from business where businessId=?");
            pstmt.setInt(1,businessId);
            pstmt.executeUpdate();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
    }

    @Override
    public Integer updatePassword(Integer businessId,String password) {
        int result = 0;
        String sql = "update business set password=? where businessId=?";
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, password);
            pstmt.setInt(2, businessId);
            result = pstmt.executeUpdate();
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DruidUtil.closeAll(null, pstmt, conn);
        }
        return result;
    }

    @Override
    public int updateBusiness(Business business) {
        int result = 0;
        String sql = "update business set businessName=?,businessAddress=?,businessExplain=?,starPrice=?,deliveryPrice=? where businessId=?";
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, business.getBusinessName());
            pstmt.setString(2, business.getBusinessAddress());
            pstmt.setString(3, business.getBusinessExplain());
            pstmt.setDouble(4, business.getStartPrice());
            pstmt.setDouble(5, business.getDeliveryPrice());
            pstmt.setInt(6, business.getBusinessId());
            result = pstmt.executeUpdate();
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DruidUtil.closeAll(null, pstmt, conn);
        }
        return result;
    }
}
